package keeto.com.vn.bms.ui.widget;

import java.util.ArrayList;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.model.User.EditAdminModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.ui.activity.SupportActivity;
import keeto.com.vn.bms.ui.adapter.IconCallAdapter;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class CallDialog extends Dialog implements OnItemClickListener {
	private Context context;
	private GridView grdApps;
	private ArrayList<Drawable> icons;
	private ArrayList<String> nameApps;
	public static EditAdminModel adminModel;
	private Listener listener= new Listener();
	private BMSSharedPreferences mPreferences;
	IconCallAdapter iconCallAdapter;

	protected CallDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		// TODO Auto-generated constructor stub
	}

	public CallDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
	}

	public CallDialog(Context context, ArrayList<Drawable> icon,
			ArrayList<String> name) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.icons = icon;
		this.nameApps = name;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.popup_contacts);
		mPreferences= new BMSSharedPreferences(context);
		grdApps = (GridView) findViewById(R.id.grd_call);
		iconCallAdapter = new IconCallAdapter(icons, nameApps,
				context);
		grdApps.setAdapter(iconCallAdapter);
		iconCallAdapter.notifyDataSetChanged();
		grdApps.setOnItemClickListener(this);
		BMSCore.getInstance(context).taskGetSupport(mPreferences.getUserAuthKey(context), listener);
	}
	

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if(nameApps.get(position).equals("Phone")){
			onPhone(view, adminModel.getPhone());
		}
		else if(nameApps.get(position).equals("Skype")){
			onSkype(view, adminModel.getSkype());
		}
		else if(nameApps.get(position).equals("Viber")){
			onViber(view, adminModel.getViber());
		}
		else if(nameApps.get(position).equals("KakaoTalk")){
			onKakao(view);
		}


	}

	public void onPhone(View v, String phoneNum) {
		try {
			Uri sUri = Uri.parse("tel:" + phoneNum);
			Intent i = new Intent(Intent.ACTION_DIAL, sUri);		
			i.setClassName("com.android.contacts",
					"com.android.contacts.DialtactsActivity");
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.addCategory(Intent.CATEGORY_LAUNCHER);
			context.startActivity(i);
		} catch (Exception e) {
			Log.e("LOG", e.toString());
			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(context, e.toString(), duration);
			toast.show();
		}
	}

	public void onKakao(View v ) {
		try {
			Intent i = new Intent();
			PackageManager manager = context.getPackageManager();
			i = manager.getLaunchIntentForPackage(SupportActivity.sKakao);
			i.addCategory(Intent.CATEGORY_LAUNCHER);
//			Uri kakaoUri = Uri.parse("kakao:" + kakao);
//			Intent myIntent = new Intent(Intent.ACTION_VIEW, kakaoUri);
//			myIntent.setClassName("com.kakao.talk", "com.kakao.talk.activity.VoiceCallFormActivity");
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.addCategory(Intent.CATEGORY_LAUNCHER);
			context.startActivity(i);
		} catch (Exception e) {

			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(context, e.toString(), duration);
//			Log.e(kakao, e.toString());
			toast.show();
		}
	}

	public void onSkype(View v, String skypeName) {
		try {
			Uri skypeUri = Uri.parse("skype:" + skypeName + "?call");
			Intent myIntent = new Intent(Intent.ACTION_VIEW, skypeUri);
			myIntent.setComponent(new ComponentName("com.skype.raider",
					"com.skype.raider.Main"));
			myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(myIntent);
		} catch (Exception e) {

			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(context, "error", duration);
			toast.show();
		}
	}

	public void onViber(View v, String dialNumber) {
		try {
			Uri uri = Uri.parse("tel:" + Uri.encode(dialNumber));
			Intent intent = new Intent("android.intent.action.VIEW");
			intent.setClassName("com.viber.voip",
					"com.viber.voip.WelcomeActivity");
			intent.setData(uri);
			context.startActivity(intent);
		} catch (Exception e) {

			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText(context, e.toString(), duration);
			toast.show();
		}
	}
	class Listener extends BMSListener{

		@Override
		public void finishGetSuport(final int returnCode, String resultMsg,
				final EditAdminModel admin) {
			super.finishGetSuport(returnCode, resultMsg, admin); 
			((Activity) context).runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if(returnCode == IBMSProtocol.STATUS_CODE_OK){
						adminModel = admin;
					}
				}
			});
		}
		
	} 

}
